
function check_mutil_form(form, opts){
	if(MUTIL_SPEC){
		//需要检查多规格的必要字段
		var has_error = false;
		var available_row = 0;
		var mutil_name_val = 0;
		var price_stock_error = false;
		var spec_names = [];
		var spec_name1 = $('input[name="Goods[spec1]"]').val();
		var spec_name2 = $('input[name="Goods[spec2]"]').val();
		if(spec_name1 != ''){
			mutil_name_val += 1;
		}
		if(spec_name2 != ''){
			mutil_name_val += 2;
		}
		if(mutil_name_val == 0 || mutil_name_val == 2 || spec_name1 == spec_name2){
			//未选或只选维度2时，报错
			$('.mutil_spec .y_selector').addClass('ext_error');
			$(opts.submit_btn).attr('disabled', 'disabled');
			$('#mutil_error').html('请设置规格维度！');
			return;
		}else{
			$('.mutil_spec .y_selector').removeClass('ext_error');
		}

		$('.mutil_spec .row').each(function(){
			if(has_error)return;
			var row_val = 0;
			var spec1 = $(this).find('input:eq(0)').val();
			var spec2 = $(this).find('input:eq(1)').val();
			if(spec1 != ''){
				row_val += 1;
			}
			if(spec2 != ''){
				row_val += 2;
			}
			if(row_val == 2){
				$(this).find('input:lt(2)').addClass('ext_error');
				has_error = true;
				$('#mutil_error').html('请设置规格维度1！');
				return;
			}

			$(this).find('input:lt(2)').removeClass('ext_error');
			if(row_val > 0){
				if(row_val != 0 && row_val != mutil_name_val){
					$(this).find('input:lt(2)').addClass('ext_error');
					has_error = true;
					$('#mutil_error').html('请设置规格维度2！');
					return;
				}

				//检查价格与库存
				if($(this).find('input:eq(2)').val() == ''){
					$(this).find('input:eq(2)').addClass('ext_error');
					has_error = true;
					price_stock_error = true;
					$('#mutil_error').html('价格不能为空！');
					return;
				}else{
					$(this).find('input:eq(2)').removeClass('ext_error');
				}
				if($(this).find('input:eq(3)').val() == ''){
					$(this).find('input:eq(3)').addClass('ext_error');
					has_error = true;
					price_stock_error = true;
					$('#mutil_error').html('库存不能为空！');
					return;
				}else{
					$(this).find('input:eq(3)').removeClass('ext_error');
				}

				//检查是否有重复的
				var c_spec_name = spec1 + '_' +  spec2;
				for(var i = 0, c = spec_names.length; i < c; ++i){
					if(spec_names[i] == c_spec_name){
						$('#mutil_error').html('规格重复！');
						$(this).find('input:lt(2)').addClass('ext_error');
						has_error = true;
						break;
					}
				}
				spec_names[spec_names.length] = c_spec_name;
				if(!has_error){
					++available_row;
				}
			}
		});

		if(available_row == 0 && !price_stock_error){
			has_error = true;
			$('.mutil_spec .y_table .row input:lt(2)').addClass('ext_error');
			$('#mutil_error').html('请至少设置一个规格值！');
		}else{
			$('.mutil_spec .y_table .row input:lt(2)').removeClass('ext_error');
		}

		if(has_error){
			$(opts.submit_btn).attr('disabled', 'disabled');
		}else{
			$('#mutil_error').html('');
			$(opts.submit_btn).removeAttr('disabled');
		}
	}else{
		if($(form).find('.error,.ext_error').length){
			$(opts.submit_btn).attr('disabled', 'disabled');
		}else{
			$('#mutil_error').html('');
			$(opts.submit_btn).removeAttr('disabled');
		}
	}
}

var auto_check_opts = {
	'submit_btn' 	: '.save_btn',
	'next_focus'	: true,
	'onSuccess' 	: check_mutil_form
};

function on_text_selector_selected(){
	check_mutil_form($(".setting_form"), auto_check_opts);
}

function set_assumer(assumer){
	if(assumer == 'seller'){
		$('.fee_dd').slideUp();
		$('input[name="Goods[assumer]"]').parents('dd').addClass('last_dd');
	}else{
		$('.fee_dd').slideDown();
		$('input[name="Goods[assumer]"]').parents('dd').removeClass('last_dd');
	}
}

$(function(){
	$(".setting_form")
		.y_setting()
		.auto_check(auto_check_opts)
		.ajax_form({
			'submit_btn' 	: '.save_btn',
			'form_position'	: null,
			'success' 		: function(form, data){
				window.location.href = '/manage/goods/edit/' + data;
			}
		});

	//注册编辑器窗口自动适应窗口大小
	addWinResize(function(){
		$('.editor .edui-editor').width($('.setting_content').width() - 20);
	});

	$('#open_specs').change(function(){
		if($(this).is(':checked')){
			//开启了多规格
			$('.no_mutil_spec').slideUp();
			$('.mutil_spec').slideDown();

			$('.mutil_spec input[name="spec1[]"]:eq(0)').set_focus({select:true});
			MUTIL_SPEC = true;
		}else{
			$('#mutil_error').html('');
			$('.no_mutil_spec').slideDown();
			$('.mutil_spec').slideUp().find('.error,.ext_error').removeClass('error').removeClass('ext_error');
			MUTIL_SPEC = false;
		}
		check_mutil_form($(".setting_form"), auto_check_opts);
	});
	if(MUTIL_SPEC){
		$('#open_specs').click();
	}
	$(document).on('click', '.mutil_spec .row', function(event){
		if($(event.target)[0].tagName == 'INPUT')return true;
		$(this).find('input:eq(0)').set_focus({select:true});
	});

	$(document).on('click', '.add_spec', function(){
		var tr = $(this).parents('tr').clone(true);
		tr.find('[next_focus]').next_focus();
		var temp_id = gen_id();
		tr.find('input:eq(0)').attr('name', 'spec1[' + temp_id + ']');
		tr.find('input:eq(1)').attr('name', 'spec2[' + temp_id + ']');
		tr.find('input:eq(2)').attr('name', 'spec_price[' + temp_id + ']');
		tr.find('input:eq(3)').attr('name', 'spec_stock[' + temp_id + ']');
		if(!tr.find('.del_spec').length){
			//需要添加删除功能
			tr.find('td:last').append(' | <a href="javascript:void(0)" class="del_spec">删除</a>');
		}
		$(this).parents('table').append(tr);
		check_mutil_form($(".setting_form"), auto_check_opts);
	});
	$(document).on('click', '.del_spec', function(){
		if(confirm('是否继续删除？')){
			var tr = $(this).parents('tr');
			tr.remove();
			check_mutil_form($(".setting_form"), auto_check_opts);
		}
	});

	//运费
	$('input[name="Goods[assumer]"]').change(function(){
		var assumer = $(this).val();
		set_assumer(assumer);
	});
	set_assumer($('input[name="Goods[assumer]"]:checked').val());

	$('input[name="Goods[fee_type]"]').change(function(){
		var fee_type = $(this).val();
		if(fee_type == 3){
			//使用模板
			$('.fee_template').slideDown();
		}else{
			$('.fee_template').slideUp();
		}
	});

	$('.auto_img').auto_img();
	$('.y_image_ul .img_li').each(function(){
		init_img_li(this);
	});
});
